<?php
class BrowseControllerBase extends VTController {
	protected $pageName = '';
	public $isMobileMode = false;
	/**	 * UserSessionVisitDataService object	 *	 * @var UserSessionVisitDataService object	 */
	public $userSessionVisitDataService = null;
	
	/**	 * PageDataService object	 *	 * @var PageDataService object	 */
	public $pageDataService = null;
	
	/**	 *	 * Current Package Option of User	 * @var PackageOptionsEntity Object	 */
	protected $currentPackageOption = NULL;
	
	/**	 *	 * Authentication Object	 * @var Authentication	 */
	protected $authentication = NULL;
	
	/**	 * DEMO mode status (User login as DEMO)	 * @var boolean	 */
	protected $isDemoMode = false;
	
	protected $paypal_enable = false;
	protected $checkout_enable = false;
	protected $googlecheckout_enable = false;
	protected $worldpay_enable = false;
	
	/**	 *	 * SettingsDataService Object	 * @var SettingsDataService Object	 */
	protected $settingsDataService = NULL;
	
	/**	 * Constructor of Controller Base	 */
	public function __construct() {
		parent::__construct ();
		$this->setPageName ( $this->pageName );
		
		// Init Authentication class		$this->authentication = new Authentication ();
		
		// Checking member has been logged in or not		if ($this->authentication->getCurrentUserId () != null) {
			$userDataService = new UserDataService ();
			$currentUser = $userDataService->getById ( $this->authentication->getCurrentUserId () ); //$authentication->getSession();			$this->smarty->assign ( 'hasLoggedIn', true );
			$this->smarty->assign ( 'loggedInName', $currentUser->fullname );
			$this->smarty->assign ( 'loggedInUserType', $currentUser->userType );
			$this->smarty->assign ( 'currentUser', $currentUser );
		}
		
		$this->settingsDataService = new SettingsDataService ();
		
		// Load Dynamic Page for Home Navigation		$this->pageDataService = new PageDataService ();
		$this->smarty->assign ( 'pageEntities', $this->pageDataService->getItems ( null ) );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'headTitle', 'Mua hội - giá rẻ bất ngờ' );
		$this->userSessionVisitDataService = new UserSessionVisitDataService ();
		//get and set cookie for city		$city = $this->getQuery ( 'city', 0 );
		if ($city != 0) {
			setcookie ( 'city', $city );
		}
		if (isset ( $_COOKIE ['city'] )) {
			$this->smarty->assign ( 'city', $_COOKIE ['city'] );
			$this->smarty->assign ( 'citys', $_COOKIE ['city'] );
		} else {
			$this->smarty->assign ( 'city', 1 );
			$this->smarty->assign ( 'citys', 1 );
		}
		//get All City
		$cityDataService = new CityDataService();
		$this->smarty->assign('citiesEntity', $cityDataService->getItems(array()));

		//get All Category
		$categoryDataService = new GrouponCategoryDataService();
		$this->smarty->assign('categoriesEntity', $categoryDataService->getItems(array()));

		// Check Mobile Mode Browsing
		$this->isMobileMode = CommonUtils::detect_mobile ();
	}
	
	/**	 * Set page name	 *	 * @param string $pageName: name of page	 */
	public function setPageName($pageName) {
		$this->smarty->assign ( 'pageName', $pageName );
	}
	
	/**	 * Set Meta Title	 */
	public function setPageTitle($title) {
		$this->smarty->assign ( 'headTitle', $title );
	}
	
	/**	 * Set Meta Keywords	 */
	public function setMetaKeywords($keywords) {
		$this->smarty->assign ( 'metaKeywords', $keywords );
	}
	
	/**	 * Set Meta Description	 */
	public function setMetaDescription($description) {
		$this->smarty->assign ( 'metaDescription', $description );
	}
	
	/**	 * Create new action key	 *	 * @return void	 */
	public function createActionKey() {
		// Create new token key		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
	}
	
	/**	 * Validate Action Key	 *	 * @return bool	 */
	public function validateActionKey() {
		// Get action key		$actionKey = $this->getPost ( 'actionKey', '' );
		
		if (! isset ( $_SESSION ['tokenKey'] ) || $actionKey == $_SESSION ['tokenKey']) {
			// Key has been validated			return true;
		}
		
		return false;
	}
	
	/**	 * Set name of style	 *	 * @param string $styleName: name of style	 */
	public function setStyleName($styleName) {
		$this->smarty->assign ( 'styleName', $styleName );
	}
	
	protected function loadAllPayment($userId = 1) {
		// Get Paypay		$paypalUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_USERNAME, $userId );
		$paypalPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_PASSWORD, $userId );
		$paypalSignatureSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_SIGNATURE, $userId );
		$paypalTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_TESTMODE, $userId );
		$paypalEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_ENABLE, $userId );
		
		// Get 2Checkout Settings		$checkoutUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_USERNAME, $userId );
		$checkoutPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_PASSWORD, $userId );
		$checkoutTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_TESTMODE, $userId );
		$checkoutEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_ENABLE, $userId );
		
		$this->checkout_enable = ( bool ) ($checkoutEnableSettingsEntity->value == "1");
		$this->paypal_enable = ( bool ) ($paypalEnableSettingsEntity->value == "1");
		
		$this->smarty->assign ( 'paypal_enable', $paypalEnableSettingsEntity->value );
		$this->smarty->assign ( 'checkout_enable', $checkoutEnableSettingsEntity->value );
	}
	
	/**	 * Check if User Logged	 */
	protected function requireUserLogin() {
		if ($this->authentication->getCurrentUserId () == null) {
			
			// Redirect to Page Not Found instead			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect ( $url );
			return - 1;
		
		} else if ($this->authentication->getSession ()->userType != ROLE_MEMBER) {
			// Logout this current user			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Browse/MyAccount/logout";
			CommonUtils::redirect ( $url );
			return - 1;
		}
	}
}